package com.taomeng.service.impl;

import com.taomeng.dao.IPermissionDao;
import com.taomeng.dao.RoleDao;
import com.taomeng.entity.Permission;
import com.taomeng.entity.Role;
import com.taomeng.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IPermissionServiceImpl implements IPermissionService {
    @Autowired
    private IPermissionDao iPermissionDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public HashSet<Permission> findUserPermissionsByLoginUserId(Long id) {
        //这里可以把角色和权限放在redis里面
        HashSet<Permission> set = new HashSet<>();
        List<Role> roles = roleDao.findRoleByUserId(id);
        for (Role role : roles) {
            List<Permission> permissions = iPermissionDao.findPermissionsByRoleId(role.getId());
            set.addAll(permissions);
        }
        return set;
    }

    @Override
    public List<Permission> getAll() {
        return iPermissionDao.getAll();
    }

    @Override
    public Set<Permission> findUserPermissionsByUserRoles(List<Role> roles) {
        HashSet<Permission> set = new HashSet<>();
        for (Role role : roles) {
            List<Permission> permissions = iPermissionDao.findPermissionsByRoleId(role.getId());
            set.addAll(permissions);
        }
        return set;
    }
}
